import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/index.vue'
import userInfoStore from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [{
    path: '/',
    redirect: sessionStorage.getItem('indexPath'),
  }, {
    path: '/home',
    redirect: sessionStorage.getItem('indexPath'),
    component: Layout,
    meta: { title: '平台管理' },
    children: [{
      path: 'permission',
      name: 'Permission',
      redirect: '/home/permission/menu',
      meta: { title: '权限管理' },
      children: [{
        path: 'menu',
        name: 'Menu',
        component: () => import('@/views/Home/Permission/Menu.vue'),
        meta: { title: '菜单管理' },
      }, {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/Home/Permission/Role.vue'),
        meta: { title: '角色管理' },
      }, {
        path: 'user',
        name: 'User',
        component: () => import('@/views/Home/Permission/User.vue'),
        meta: { title: '用户管理' },
      }]
    }, {
      path: 'product',
      name: 'Product',
      redirect: '/home/product/category',
      meta: { title: '产品管理' },
      children: [{
        path: 'category',
        name: 'Category',
        component: () => import('@/views/Home/Product/Category.vue'),
        meta: { title: '产品分类' },
      }, {
        path: 'catalog',
        name: 'Catalog',
        component: () => import('@/views/Home/Product/Catalog.vue'),
        meta: { title: '产品目录' },
      }]
    }, {
      path: 'supplier',
      name: 'Supplier',
      redirect: '/home/supplier/list',
      meta: { title: '供应商管理' },
      children: [{
        path: 'list',
        name: 'sList',
        component: () => import('@/views/Home/Supplier/Supplier.vue'),
        meta: { title: '供应商列表' },
      },{
        path: 'case',
        name: 'Case',
        component: () => import('@/views/Home/Supplier/Case.vue'),
        meta: { title: '供应情况' },
      }]
    }, {
      path: 'iot',
      name: 'Iot',
      component: () => import('@/views/Home/Device/Device.vue'),
      meta: { title: '物联设备' }
    }, {
      path: 'land',
      name: 'Land',
      redirect: '/home/land/list',
      meta: { title: '农场管理' },
      children: [{
        path: 'list',
        name: 'List',
        component: () => import('@/views/Home/Land/List.vue'),
        meta: { title: '农场列表' }
      }, {
        path: 'plant',
        name: 'Plant',
        component: () => import('@/views/Home/Land/Plant.vue'),
        meta: { title: '种植管理' }
      }, {
        path: 'animal',
        name: 'Animal',
        component: () => import('@/views/Home/Land/Animal.vue'),
        meta: { title: '养殖管理' }
      }]
    },]
  }, {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  }, {
    path: '/bigData',
    name: 'BigData',
    component: () => import('@/views/BigData/index.vue'),
    meta: { title: '大数据' }
  }, {
    path: '/rpc',
    name: 'RPC',
    component: () => import('@/views/RPC/index.vue'),
    meta: { title: '智慧农场' }
  },
  // 404
  {
    path: '/:pathMatch(.*)*',
    name: "NotFound",
    component: () => import('@/views/404.vue')
  }, {
    path: '/home/:catchAll(.*)',
    component: Layout,
    children: [{ path: '', component: () => import('@/views/404.vue') }]
  }, {
    path: '/farm/:catchAll(.*)',
    component: Layout,
    children: [{ path: '', component: () => import('@/views/404.vue') }]
  },
  ]
})

router.beforeEach((to, from, next) => {
  // 未登录
  if (!sessionStorage.token && to.path != '/login') {
    return next({ path: '/login' })
  }
  // 已登录
  if (sessionStorage.token && to.path == '/login') {
    return next({ path: from.path ? from.path : sessionStorage.getItem('indexPath') })
  }
  // 是否存在TODO
  if (sessionStorage.token) {
    const whiteList = userInfoStore().whiteList
    // console.log(whiteList);
    // if (whiteList.findIndex(item => item.path == to.path) == -1) {
    //   return next({path: '/'})
    // }
  }
  next()
});

export default router
